package com.example.myapplication.moudule.home.provider;

import com.example.myapplication.ResourceTable;
import ohos.agp.components.*;

import java.util.List;

public class HomeShopProvider extends BaseItemProvider {

    private List<String> data;

    public void setData(List<String> data) {
        this.data = data;
        notifyDataChanged();
    }

    public HomeShopProvider() {
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int i) {
        return data == null ? null : data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {

        final Component cpt;
        final ComponentHolder componentHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(componentContainer.getContext()).parse(ResourceTable.Layout_item_home,null,false);
            componentHolder=new ComponentHolder(cpt);
            cpt.setTag(componentHolder);
        } else {
            componentHolder= (ComponentHolder) component.getTag();
            cpt=component;
        }
        componentHolder.setData(data.get(i));
        return cpt;
    }

    class ComponentHolder {

        public Text content;
        public ComponentHolder(Component component) {
            content= (Text) component.findComponentById(ResourceTable.Id_txt_content);
        }

        public void setData(String s){
            content.setText(s);
        }
    }
}
